import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  Widget build(BuildContext context) {
    //也可以通过定义常量.value 的方式获取区间
    final Animation opacityAnimation =
        Tween(begin: 0.5, end: 0.8).animate(_controller);

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        //通过AnimatedBuilder 可以每一帧都重绘控件
        child: AnimatedBuilder(
          animation: _controller,
          builder: (BuildContext context, Widget? child) {
            return Opacity(
              opacity: opacityAnimation.value,
              child: Container(
                width: 300,
                // height: 200 + 100 * _controller.value,
                height: Tween(begin: 200.0, end: 300.0)
                    .evaluate(_controller), //通过 evaluate 来在普通控件使用动画的tween
                color: Colors.blue,
                child: child,
              ),
            );
          },
          //child 中的控件不会重新渲染
          child: const Center(
            child: Text(
              "Hi",
              style: TextStyle(fontSize: 72),
            ),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _controller.stop();
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    )..repeat(reverse: true);
    // _controller.addListener(() {
    //   print("${_controller.value}");
    // });
  }

  @override
  void dispose() {
    _controller.dispose();
  }
}
